package com.zmc.product.controller;

import com.zmc.common.domain.query.goods.GoodSkuSearchFeignQuery;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.product.application.GoodsSkuApplicationService;
import com.zmc.product.infrastructure.entity.CommodityGoodsSkuEntity;
import com.zmc.product.infrastructure.bean.query.CommodityGoodsSkuQuery;
import com.zmc.product.infrastructure.service.CommodityGoodsSkuService;
import com.zmc.product.infrastructure.bean.vo.GoodSkuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 商品sku管理
 * @author zmc
 */
@RestController
@RequestMapping("/sku")
public class GoodsSkuController {

    @Autowired
    private CommodityGoodsSkuService goodsSkuService;

    @Autowired
    private GoodsSkuApplicationService goodsSkuApplicationService;


    /**
     * 查询商品sku列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CommodityGoodsSkuQuery query) {
        return goodsSkuApplicationService.pageCommodityGoodsSkuList(query);
    }


    /**
     * 导出商品sku列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityGoodsSkuQuery query) {
        List<CommodityGoodsSkuEntity> list = goodsSkuService.selectCommodityGoodsSkuList(query);
        ExcelUtil<CommodityGoodsSkuEntity> util = new ExcelUtil<>(CommodityGoodsSkuEntity.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取商品详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody GoodSkuVo goodSkuVo) {
        GoodSkuVo vo = goodsSkuService.getSkuInfoBySkuId(goodSkuVo.getSkuId());
        return R.ok(vo);
    }

    /**
     * 新增商品
     */
    @PostMapping("/add")
    public R add(@RequestBody GoodSkuVo vo) {
        goodsSkuApplicationService.saveSku(vo);
        return R.ok();
    }

    /**
     * 修改商品
     */
    @PostMapping("/update")
    public R edit(@RequestBody GoodSkuVo vo) {
        goodsSkuApplicationService.updateSkuById(vo);
        return R.ok();
    }

    /**
     * 删除商品
     */
    @PostMapping("/delete")
    public R remove(@RequestBody GoodSkuVo vo) {
        goodsSkuApplicationService.removeGoodsSku(vo.getSkuId());
        return R.ok();
    }

    /**
     * 查询所有商品sku列表
     */
    @PostMapping("/collect")
    public R skuList(@RequestBody CommodityGoodsSkuQuery query) {
        PageUtils page = goodsSkuService.pageGoodsSkuList(query);
        return R.page(page);
    }

    /**
     * 通过sku查询name
     */
    @RequestMapping("/code")
    public String getSkuNameByCode(@RequestParam("skuId") String skuId) {
        return goodsSkuApplicationService.getSkuNameBySkuId(skuId);
    }


    /*
    * 查询sku列表详情
    * */
    @PostMapping("/getBySkus")
    public R getBySkus(@RequestBody List<String> skuIds) {
        return R.ok(goodsSkuService.collect(skuIds));
    }

    /*
     * 查询sku列表详情
     * */
    @PostMapping("/search")
    public R search(@RequestBody GoodSkuSearchFeignQuery query) {
        return R.page(goodsSkuApplicationService.search(query));
    }
}
